<template>
  <div>
    <el-card style="margin: 20px">
      <el-row :gutter="20">
        <el-col :span="18">
          <el-input
            placeholder="根据用户名搜索"
            v-model="form.subjectName"
            style="width: 200px; margin-right: 8px"
          ></el-input>
          <el-button size="small" @click="emply">清空</el-button>
          <el-button type="primary" size="small" @click="handelSearch"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="handelAdd"
            >新增用户</el-button
          >
        </el-col>
      </el-row>
      <el-alert
        title="共1条记录"
        type="info"
        show-icon
        style="margin-top: 20px"
      >
      </el-alert>
      <el-table style="width: 100%; margin-top: 20px" :data="subjectsData">
        <el-table-column label="序号" width="48px" prop="id" align="center">
        </el-table-column>
        <el-table-column
          label="学科名称"
          width="140px"
          prop="subjectName"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="创建者"
          width="130px"
          prop="username"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="创建日期"
          width="160px"
          prop="addDate"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="前台是否显示"
          width="120px"
          align="center"
        >
        <template #default="scope">
          {{scope.row.isFrontDisplay === 1 ?'是' :'否'}}
        </template>
        </el-table-column>
        <el-table-column
          label="二级目录"
          width="140px"
          prop="twoLevelDirectory"
          align="center"
        >
        </el-table-column>
        <el-table-column label="标签" width="140px" prop="tags" align="center">
        </el-table-column>
        <el-table-column
          label="题目数量"
          width="140px"
          prop="totals"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" width="240px" align="center">
          <template #default="scope">
            <el-button type="text" @click="handelDir(scope.row)">学科分类 </el-button>
            <el-button type="text" @click="handeltags(scope.row)">学科标签</el-button>
            <el-button type="text" @click="handelEdit(scope.row)">修改</el-button>
            <el-button type="text" @click="handelDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <PaginAtion
        @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange"
        :counts="counts"
        :pagesize="params.pagesize"
      ></PaginAtion>
      <!--新增和修改 -->
      <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
        <el-form label-width="80px" :data="subjectAdd">
          <el-form-item label="学科名称">
            <el-input
              placeholder="请输入学科名称"
              style="width: 280px"
              v-model="subjectAdd.subjectName"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="subjectAdd.isFrontDisplay"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            <!-- <template #default="scope">
              {{scope.row.isFrontDisplay === 1?1:0}}
            </template> -->
            </el-switch>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/subjects'
import PaginAtion from '@/components/PaginAtion/index'
export default {
  components: {
    PaginAtion
  },
  data () {
    return {
      params: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      subjectsData: [],
      counts: 0,
      form: {
        subjectName: ''
      },
      subjectAdd: {
        subjectName: ''
      },
      title: '',
      dialogVisible: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleSizeChange (val) {
      this.params.pagesize = val
      this.getList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(val)
      this.params.page = val
      this.getList()
      console.log(`当前页: ${val}`)
    },
    async getList () {
      const res = await list(this.params)
      console.log(res)
      this.subjectsData = res.data.items
      this.counts = res.data.counts
    },
    // 清空
    emply () {
      this.form = {}
      this.getList()
    },
    // 搜索
    async handelSearch () {
      this.params.subjectName = this.form.subjectName
      await list(this.params)
      this.getList()
    },
    // 新增
    handelAdd () {
      this.title = '新增学科'
      this.dialogVisible = true
    },
    async save () {
      if (this.title === '新增学科') {
        const res = await add(this.subjectAdd)
        console.log(res)
        this.getList()
        this.dialogVisible = false
        this.subjectAdd = {}
      } else {
        console.log(this.subjectAdd)
        if (this.subjectAdd.isFrontDisplay === true) {
          this.subjectAdd.isFrontDisplay = 1
        } else {
          this.subjectAdd.isFrontDisplay = 0
        }
        const res = await update(this.subjectAdd)
        console.log(res)
        this.getList()
        this.dialogVisible = false
        this.subjectAdd = {}
      }
    },
    // 跳转
    handelDir (row) {
      this.$router.push({
        path: 'directorys',
        query: {
          id: row.id
        }
      })
    },
    handeltags (row) {
      this.$router.push({
        path: 'tags',
        query: {
          id: row.id
        }
      })
    },
    // 修改
    handelEdit (row) {
      console.log(row)
      this.title = '修改学科'
      this.dialogVisible = true
      this.subjectAdd.subjectName = row.subjectName
      this.subjectAdd.id = row.id
      if (row.isFrontDisplay === 1) {
        this.subjectAdd.isFrontDisplay = true
      } else {
        this.subjectAdd.isFrontDisplay = false
      }
    },
    // 删除
    async handelDel (id) {
      await this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      const res = await remove(id)
      console.log(res)
      this.$message({
        type: 'success',
        message: '成功删除用户!'
      })
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss"></style>
